package com.fengba.nanzhi.tabbar01;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.MotionEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;

import com.fengba.nanzhi.fengba.R;
import com.fengba.nanzhi.notice.Notice;
import com.fengba.nanzhi.notice.NoticeAdapter;
import com.fengba.nanzhi.tabbar01.fx.Touchview.ISlide;
import com.fengba.nanzhi.tabbar01.fx.Touchview.OnClickSlideItemListener;
import com.fengba.nanzhi.tabbar01.fx.Touchview.SlideRecyclerViewBaseAdapter;
import com.fengba.nanzhi.utils.OKManager;
import com.fengba.nanzhi.utils.Preferences;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;


public class FX_SQ extends Activity {

    private String type_;

    private EditText SQet01;

    private TextView SQtv05;

    private JSONArray jsonArray1;

    private JSONArray jsonArray2;

    private JSONArray jsonArray3;

    private TextView SQshdztitle;
    private SlideRecyclerViewBaseAdapter adapter;


    public FX_SQ() {

    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.fx_fdsq);
        SQshdztitle = (TextView) findViewById(R.id.SQshdztitle);
        Intent intent = getIntent();
        type_ = intent.getStringExtra("type");
        SQet01 = (EditText) findViewById(R.id.SQet01);
        SQet01.setInputType(EditorInfo.TYPE_CLASS_PHONE);


        /**
         * 返回
         */
        ImageButton SQbtn01 = (ImageButton)findViewById(R.id.SQbtn01);
        SQbtn01.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        /**
         * 搜索
         */
        SQtv05 = (TextView) findViewById(R.id.SQtv05);
        TextView SQtv01 = (TextView)findViewById(R.id.SQtv01);
        SQtv01.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                SQtv05.setText("搜索结果");
                init_();

            }
        });
        if (type_.equals("2"))
        {
            SQshdztitle.setText("赠送疯豆");
            SQtv05.setText("索取列表");

        }
        final RefreshLayout refreshLayout = (RefreshLayout) findViewById(R.id.SQrefreshLayout);
        refreshLayout.setOnRefreshListener(new OnRefreshListener() {

            public void onRefresh(RefreshLayout refreshlayout) {
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        if (type_.equals("2"))
                        {
                            _init();
                        }else
                        {
                            init();
                        }
                        if (jsonArray1 == null) {
                            Toast.makeText(FX_SQ.this, "已经没有更多数据了", Toast.LENGTH_SHORT).show();
                            refreshLayout.finishRefresh();
                        } else {
                            Toast.makeText(FX_SQ.this, "查询成功", Toast.LENGTH_SHORT).show();
                            refreshLayout.finishRefresh();
                        }
                        /**
                         * 刷新完成后调用此方法，要不然刷新效果不消失
                         */

                    }
                }, 0);
            }
        });
        refreshLayout.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(RefreshLayout refreshlayout) {
                refreshlayout.finishLoadMore(2000/*,false*/);//传入false表示加载失败
            }
        });

        if (type_.equals("2"))
        {
            _init();
        }else
         {
          init();
         }

    }//都在这里打

    /**
     * 隐藏软键盘
     *
     * @param ev
     * @return
     */
    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        if (ev.getAction() == MotionEvent.ACTION_DOWN) {
            View v = getCurrentFocus();
            if (isShouldHideInput(v, ev)) {

                InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                if (imm != null) {
                    imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
                }
            }
            return super.dispatchTouchEvent(ev);
        }
        // 必不可少，否则所有的组件都不会有TouchEvent了
        if (getWindow().superDispatchTouchEvent(ev)) {
            return true;
        }
        return onTouchEvent(ev);
    }

    public boolean isShouldHideInput(View v, MotionEvent event) {
        if (v != null && (v instanceof EditText)) {
            int[] leftTop = {0, 0};
            //获取输入框当前的location位置
            v.getLocationInWindow(leftTop);
            int left = leftTop[0];
            int top = leftTop[1];
            int bottom = top + v.getHeight();
            int right = left + v.getWidth();
            if (event.getX() > left && event.getX() < right
                    && event.getY() > top && event.getY() < bottom) {
                // 点击的是输入框区域，保留点击EditText的事件
                return false;
            } else {
                return true;
            }
        }
        return false;
    }




    private RecyclerView mCardyhk;

    public void init() {
        OKManager.getInstance().type2007("310", new OKManager.func4() {
            @Override
            public void onResponse(JSONObject jsonObject) {
                try {
                    if (jsonObject.get("code").equals(0)) {
                        jsonArray1 = jsonObject.getJSONArray("data");
                        ////System.out.println("JA"+jsonArray1.toString());
                        mCardyhk = findViewById(R.id.SQshdzlb);
                        mCardyhk.setLayoutManager(new LinearLayoutManager(FX_SQ.this));
                        mCardyhk.setAdapter(new FXSQAdapter(FX_SQ.this,FX_SQ.this,type_));
                    } else {
                        Toast.makeText(FX_SQ.this, jsonObject.get("message").toString(), Toast.LENGTH_SHORT).show();
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                //refreshLayout.finishRefresh();
            }
            @Override
            public void onFailure(String string){
                Toast.makeText(FX_SQ.this, "网络错误", Toast.LENGTH_SHORT).show();
                //refreshLayout.finishRefresh();
            }
        });

    }
    public JSONArray getJsonArray() {
        return jsonArray1;
    }

    public void init_()
    {
        String id = null;
        try {
            Object user_info = Preferences.getParam(FX_SQ.this, "user_info", "");
            JSONObject myJson = null;
            myJson = new JSONObject(user_info.toString());
            id = myJson.get("id").toString();
        } catch (JSONException e) {
            e.printStackTrace();
        }
        OKManager.getInstance().type2009(id,String.valueOf(SQet01.getText()), new OKManager.func4() {
            @Override
            public void onResponse(JSONObject jsonObject) {
                try {
                    if (jsonObject.get("code").equals(0)) {
                        jsonArray2=jsonObject.getJSONArray("data");
                        ////System.out.println("jsonArray2"+jsonArray2.toString());
                        mCardyhk = findViewById(R.id.SQshdzlb);
                        mCardyhk.setLayoutManager(new LinearLayoutManager(FX_SQ.this));
                        mCardyhk.setAdapter(new FXSQAdapter2(FX_SQ.this,FX_SQ.this,type_));
                    } else {
                        Toast.makeText(FX_SQ.this, jsonObject.get("message").toString(), Toast.LENGTH_SHORT).show();
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                //refreshLayout.finishRefresh();
            }
            @Override
            public void onFailure(String string){
                Toast.makeText(FX_SQ.this, "网络错误", Toast.LENGTH_SHORT).show();
                //refreshLayout.finishRefresh();
            }
        });
    }
    public JSONArray getJsonArray2() {
        return jsonArray2;
    }


    public void _init()
    {
        String account = null;
        try {
            Object user_info = Preferences.getParam(FX_SQ.this, "user_info", "");
            JSONObject myJson = null;
            myJson = new JSONObject(user_info.toString());
            account = myJson.get("account").toString();
        } catch (JSONException e) {
            e.printStackTrace();
        }
        OKManager.getInstance().type2010(account,"1","100", new OKManager.func4() {
            @Override
            public void onResponse(JSONObject jsonObject) {
                try {
                    if (jsonObject.get("code").equals(0)) {
                        jsonArray3 = jsonObject.getJSONArray("data");
                        mCardyhk = findViewById(R.id.SQshdzlb);
                        mCardyhk.setLayoutManager(new LinearLayoutManager(FX_SQ.this));
                        mCardyhk.setAdapter(new FXSQAdapter3(FX_SQ.this,FX_SQ.this));
                    } else {
                        Toast.makeText(FX_SQ.this, jsonObject.get("message").toString(), Toast.LENGTH_SHORT).show();
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                //refreshLayout.finishRefresh();
            }
            @Override
            public void onFailure(String string){
                Toast.makeText(FX_SQ.this, "网络错误", Toast.LENGTH_SHORT).show();
                //refreshLayout.finishRefresh();
            }
        });
    }
    public JSONArray getJsonArray3() {
        return jsonArray3;
    }

}
